import Vue from 'vue'
import { getDicts } from '@/api/system/dict/data'

export default class Dict {
  // 有参构造
  constructor(dict) {
    this.dict = dict
  }

  /**
   * 初始化数据
   * @param {*} dictType 字典类型
   * @returns Promise
   */
  initData(dictType) {
    if (dictType === undefined || dictType === null) {
      return Promise.reject('字典类型无效')
    }
    const dataInfo = []
    if (dictType instanceof Array) {
      dictType.forEach(t => {
        Vue.set(this.dict.dict, t, [])
        Vue.set(this.dict.label, t, {})
        dataInfo.push(getDicts(t).then(response => {
          this.dict.dict[t].splice(0, 0, ...response.data)
          response.data.forEach(n => {
            Vue.set(this.dict.label[t], n.dictValue, n.dictLabel)
          })
          return response.data
        }))
      })
      return Promise.all(dataInfo)
    }
  }
}
